<template>
    <div class="app-container">
        <el-form :model="HospitalSet" :rules="rules" ref="HospitalSet" label-width="100px" class="demo-ruleForm">
            <el-form-item label="医院名称" prop="hosname">
                <el-input v-model="HospitalSet.hosname"></el-input>
            </el-form-item>

            <el-form-item label="医院编号" prop="hoscode">
                <el-input v-model="HospitalSet.hoscode"></el-input>
            </el-form-item>

            <el-form-item label="医院网址" prop="apiUrl">
                <el-input v-model="HospitalSet.apiUrl"></el-input>
            </el-form-item>

            <el-form-item label="联系人姓名" prop="contactsName">
                <el-input v-model="HospitalSet.contactsName"></el-input>
            </el-form-item>

            <el-form-item label="联系人手机" prop="contactsPhone">
                <el-input v-model="HospitalSet.contactsPhone" maxlength="11"></el-input>
            </el-form-item>
            
            <el-form-item>
                <el-button v-if="HospitalSet.id != null" type="primary" :disabled="butt" @click="submitForm('HospitalSet')">立即修改</el-button>
                <el-button v-else type="primary" :disabled="butt" @click="submitForm('HospitalSet')">立即添加</el-button>
                <el-button @click="resetForm('HospitalSet')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
    import hospitalSet from '@/api/hospitalSet'

    export default {
        data(){
            var validateHoscode = (rule, value, callback) => {
                if (!(/^\w+$/.test(value))) {
                    callback(new Error('请输入由数字、26个英文字母或者下划线组成的编号'));
                } else {
                    callback();
                }
            };
            var validateApiUrl = (rule, value, callback) => {
                if (!(/[a-zA-z]+:\/\/[^\s]*/.test(value))) {
                    callback(new Error('请输入正确的网址(例:http://)'));
                } else {
                    callback();
                }
            };
            var validateContactsName = (rule, value, callback) => {
                if (!(/^[\u4E00-\u9FA5A-Za-z0-9]+$/.test(value))) {
                    callback(new Error('请输入正确的姓名'));
                } else {
                    callback();
                }
            };
            var validateContactsPhone = (rule, value, callback) => {
                if (!(/^1((34[0-8])|(8\d{2})|(([35][0-35-9]|4[579]|66|7[35678]|9[1389])\d{1}))\d{7}$/.test(value))) {
                    callback(new Error('请输入正确的手机号'));
                } else {
                    callback();
                }
            };
            return{
                butt:false,
                HospitalSet: {
                
                },
                rules: {
                    hosname: [
                        { required: true, message: '请输入医院名称', trigger: 'blur' },
                        { min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ],
                    hoscode: [
                        { required: true, message: '请输入医院编号', trigger: 'blur' },
                        { validator: validateHoscode, trigger: 'blur' }
                    ],
                    apiUrl: [
                        { required: true, message: '请输入医院网址', trigger: 'blur' },
                        { validator: validateApiUrl, trigger: 'blur' }
                    ],
                    contactsName: [
                        { required: true, message: '请输入联系人姓名', trigger: 'blur' },
                        { validator: validateContactsName, trigger: 'blur' }
                    ],
                    contactsPhone: [
                        { required: true, message: '请输入联系人电话', trigger: 'blur' },
                        { validator: validateContactsPhone, trigger: 'blur' }
                    ]
                    
                }
            }
        },
        methods:{
            submitForm(HospitalSet) {
                
                this.$refs[HospitalSet].validate((valid) => {
                    if (valid) {
                        this.butt=true;
                        if(!this.HospitalSet.id){
                            hospitalSet.save(this.HospitalSet).then(resp=>{
                                this.$message({
                                    message: '添加信息成功!',
                                    type: 'success'
                                });
                                this.$router.push('/yygh/hospitalSet/list');
                            });
                        }else{
                            hospitalSet.update(this.HospitalSet).then(resp=>{
                                this.$message({
                                    message: '修改信息成功!',
                                    type: 'success'
                                });
                                this.$router.push('/yygh/hospitalSet/list');
                            })
                        }
                    } else {
                        this.$alert('请检查表单内容', '提交错误', {
                            confirmButtonText: '确定',
                            callback: action => {
                                this.$message({
                                type: 'info',
                                message: `action: ${ action }`
                                });
                            }
                        });
                        return false;
                    }
                });
            },
            resetForm(HospitalSet) {
                this.$refs[HospitalSet].resetFields();
            }
        },
        created(){
            if(this.$route.params && this.$route.params.id){
                hospitalSet.getById(this.$route.params.id).then(resp=>{
                    this.HospitalSet = resp.data.item;
                })
            }
        }

    }
</script>
